<template>
  <div class="login">
    <div class="login-container">
      <div class="form-box">
        <!-- Logo和标题 -->
        <div class="logo-title">
          <img :src="basic.logoBlack" alt="logo" />
          <!-- <h2>{{ basic.title }}</h2> -->
        </div>

        <!-- 切换标签 -->
        <div class="form-tabs">
          <div
            class="tab-item"
            :class="{ active: !isRegister }"
            @click="isRegister = false"
          >
            登录
          </div>
          <div
            class="tab-item"
            :class="{ active: isRegister }"
            @click="isRegister = true"
          >
            注册
          </div>
        </div>

        <!-- 登录表单 -->
        <el-form
          ref="loginRef"
          v-if="!isRegister"
          :model="loginParams"
          :rules="rules"
        >
          <el-form-item prop="username">
            <el-input placeholder="请输入用户名" v-model="loginParams.username">
              <template #prefix>
                <el-icon :size="20">
                  <UserFilled />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              v-model="loginParams.password"
              type="password"
            >
              <template #prefix>
                <el-icon :size="20">
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <el-input placeholder="请输入验证码" v-model="loginParams.captcha">
              <template #prefix>
                <el-icon :size="20">
                  <ChromeFilled />
                </el-icon>
              </template>
              <template #append>
                <div
                  class="captcha-img"
                  v-html="captcha"
                  @click="changeCaptcha"
                ></div>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="submit-btn" type="primary" @click="login">
              登录
            </el-button>
          </el-form-item>
        </el-form>

        <!-- 注册表单 -->
        <el-form
          ref="formRef"
          v-else
          :model="registerParams"
          :rules="registerRules"
        >
          <el-form-item prop="头像">
            <avatar-upload
              v-model="registerParams.avatar"
              @change="handleAvatarChange"
            />
          </el-form-item>
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              v-model="registerParams.username"
            >
              <template #prefix>
                <el-icon :size="20">
                  <UserFilled />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="nickName">
            <el-input
              placeholder="请输入昵称"
              v-model="registerParams.nickName"
            >
              <template #prefix>
                <el-icon :size="20">
                  <UserFilled />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="introduction">
            <el-input
              placeholder="自我介绍"
              v-model="registerParams.introduction"
            >
              <template #prefix>
                <el-icon :size="20">
                  <UserFilled />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              v-model="registerParams.password"
              type="password"
            >
              <template #prefix>
                <el-icon :size="20">
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input
              placeholder="请确认密码"
              v-model="registerParams.confirmPassword"
              type="password"
            >
              <template #prefix>
                <el-icon :size="20">
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <el-input
              placeholder="请输入验证码"
              v-model="registerParams.captcha"
            >
              <template #prefix>
                <el-icon :size="20">
                  <ChromeFilled />
                </el-icon>
              </template>
              <template #append>
                <div
                  class="captcha-img"
                  v-html="captcha"
                  @click="changeCaptcha"
                ></div>
              </template>
            </el-input>
          </el-form-item>
          <el-button class="submit-btn" type="primary" @click="handleRegister">
            注册
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { message } from "@/Hooks/Element-plus"
import AvatarUpload from "@/components/Upload/index.vue"
import { basic } from "../../config/setting"
import { useUserStore } from "@/stores/modules/user"
import { reactive, onMounted, ref } from "vue"
import { useRoute, useRouter } from "vue-router"
import { getCaptcha } from "@/api/login"
import { UserFilled, Lock, Message, Plus } from "@element-plus/icons-vue"
import type { UploadProps } from "element-plus"
import { delAvatar } from "@/api/file-manage"
import { register } from "@/api/user"
const imageUrl = ref("")

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const isRegister = ref(false)
const captcha = ref("")
const formRef = ref()
const loginRef = ref()
const loginParams = reactive({
  username: "xiajq",
  password: "xxjjqq",
  captcha: "",
})

const validateUser = (rule: any, value: any, callback: any) => {
  if (value.length < 4) {
    callback(new Error("用户名长度必须大于4位"))
  } else {
    callback()
  }
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    callback(new Error("密码长度必须大于6位"))
  } else {
    callback()
  }
}

const rules = reactive({
  username: [{ required: true, validator: validateUser, trigger: "change" }],
  password: [{ required: true, validator: validatePass, trigger: "change" }],
})

// 注册相关
const registerParams = reactive({
  avatar: "",
  username: "",
  password: "",
  confirmPassword: "",
  captcha: "",
  nickName: "",
  introduction: "",
})

const registerRules = {
  avatar: [{ required: true, message: "请上传头像", trigger: "blur" }],
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 4, max: 20, message: "长度在 4 到 20 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, message: "请确认密码", trigger: "blur" },
    {
      validator: (rule: any, value: string, callback: Function) => {
        if (value !== registerParams.password) {
          callback(new Error("两次输入密码不一致"))
        } else {
          callback()
        }
      },
      trigger: "blur",
    },
  ],
  captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
}
const handleAvatarChange = (url: string) => {
  registerParams.avatar = url
}
const changeCaptcha = () => {
  getCaptcha().then((res: any) => {
    captcha.value = res
  })
}

onMounted(() => {
  changeCaptcha()
})

// 保持原有登录逻辑
const login = async () => {
  await loginRef.value.validate()
  try {
    const mes = await userStore.loginUp(loginParams)
    router.push({ path: route.query.url as any })
  } catch (err: any) {
    changeCaptcha()
  }
}

// 注册处理
const handleRegister = async () => {
  await formRef.value.validate()
  try {
    const res = await register(registerParams)
    if (res.code === 200) {
      message("success", "注册成功")
      isRegister.value = false
      changeCaptcha()
    }
  } catch (err: any) {
    changeCaptcha()
  }
}
</script>

<style lang="scss" scoped>
// 注册

.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

  .login-container {
    width: 100%;
    max-width: 1200px;
    margin: 0 20px;
    display: flex;
    justify-content: center;
  }

  .form-box {
    width: 100%;
    max-width: 420px;
    padding: 40px;
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(8px);

    .logo-title {
      text-align: center;
      margin-bottom: 30px;

      img {
        width: 180px;
        height: 80px;
        margin-bottom: 15px;
      }

      h2 {
        font-size: 24px;
        color: #333;
        font-weight: 600;
      }
    }

    .form-tabs {
      display: flex;
      justify-content: center;
      margin-bottom: 30px;
      border-bottom: 1px solid #eee;

      .tab-item {
        padding: 12px 30px;
        margin: 0 15px;
        font-size: 16px;
        color: #666;
        cursor: pointer;
        position: relative;
        transition: all 0.3s ease;

        &::after {
          content: "";
          position: absolute;
          bottom: -1px;
          left: 0;
          width: 100%;
          height: 2px;
          background-color: var(--el-color-primary);
          transform: scaleX(0);
          transition: transform 0.3s ease;
        }

        &:hover {
          color: var(--el-color-primary);
        }

        &.active {
          color: var(--el-color-primary);
          font-weight: 500;

          &::after {
            transform: scaleX(1);
          }
        }
      }
    }

    .el-form {
      .el-form-item {
        margin-bottom: 25px;

        &:last-child {
          margin-bottom: 0;
        }
      }

      :deep(.el-input__wrapper) {
        background-color: #f5f7fa;
        box-shadow: none;
        border: 1px solid transparent;
        transition: all 0.3s ease;
        height: 45px;

        &:hover,
        &.is-focus {
          background-color: #fff;
          border-color: var(--el-color-primary);
          box-shadow: 0 0 0 1px var(--el-color-primary-light-8);
        }
      }

      .el-input__prefix {
        margin-left: 5px;
      }
    }

    .submit-btn {
      width: 100%;
      height: 45px;
      font-size: 16px;
      font-weight: 500;
      letter-spacing: 1px;
      background: linear-gradient(
        45deg,
        var(--el-color-primary),
        var(--el-color-primary-light-3)
      );
      border: none;
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
      }

      &:active {
        transform: translateY(0);
      }
    }
  }
}

// 验证码样式
.captcha-img {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;

  :deep(svg) {
    height: 100%;
  }
}

// 响应式处理
@media screen and (max-width: 576px) {
  .login {
    .form-box {
      padding: 30px 20px;

      .form-tabs {
        .tab-item {
          padding: 10px 20px;
          font-size: 14px;
        }
      }
    }
  }
}
</style>
